Ext.namespace("Milan.widget");
Milan.widget.virtualInputArray = {};
Milan.widget.VirtualInput = Class.create();
Milan.widget.VirtualInput.prototype={
	initialize : function(id)
	{
		if(id){
			this.id = id;
		}else{
			this.id = "input_"+new Date().getTime();
		}
		this.isIE=Milan.navigator.isIE;//(!-[1,]);
		this.name = this.id;
		this.defaultValue = "";
		this.divClassName="div_input";
		this.className = "input";
		this.width=120;
		this.title="";
		this.readOnly = false;
		this.btn1Id = "";
		this.btn1Class="search_btn1";
		this.btn1Value="";
		this.btn1Display="none";
		this.isTwoBtn=true;
		this.btn2Id="";
		this.btn2Class="search_btn2";
		this.btn2Value="";
		this.btn2Display="none";
		this.setId=function(id){this.id=id};
		this.setName=function(name){this.name=name};
		this.setDefaultValue=function(defaultValue){this.defaultValue=defaultValue};
		this.getDefaultValue=function(){return this.defaultValue;};
		this.setDivClassName=function(divClassName){this.divClassName=divClassName};
		this.setClassName=function(className){this.className=className};
		this.setWidth=function(width){this.width=parseInt(width);};
		this.setTitle=function(title){this.title=title};
		this.setReadOnly=function(readOnly){this.readOnly=readOnly};
		this.setBtn1Id=function(btn1Id){this.btn1Id=btn1Id};
		this.setBtn1Class=function(btn1Class){this.btn1Class=btn1Class};
		this.setBtn1Value=function(btn1Value){if(btn1Value)this.btn1Value=btn1Value};
		this.setBtn1Display=function(btn1Display){this.btn1Display=btn1Display};
		this.setIsTwoBtn=function(isTwoBtn){this.isTwoBtn=isTwoBtn};
		this.setBtn2Id=function(btn2Id){this.btn2Id=btn2Id};
		this.setBtn2Class=function(btn2Class){this.btn2Class=btn2Class};
		this.setBtn2Value=function(btn2Value){if(btn2Value)this.btn2Value=btn2Value};
		this.setBtn2Display=function(btn2Display){this.btn2Display=btn2Display};
		this.setPlaceholder=function(placeholder){this.placeholder=(typeof placeholder == "string")?$(placeholder):placeholder;};//设置占位元素
		this.getInput=function(){return this.input;};
		this.getInnerInput=function(){return this.innerInput;};
		this.getBtn2=function(){return this.btn2;};
		this.getBtn1=function(){return this.btn1;};
	},
	transfer:function(placeholder){
		this.createInput();
		if(placeholder){
			this.placeholder=placeholder;
		}
		if(this.placeholder){
			Element.replace(this.placeholder,this.input);
		}
		Milan.widget.virtualInputArray[this.id]=this;
	},
	createInput:function(){
		this.input=document.createElement("DIV");
		this.input.className=this.divClassName;
		if(this.isIE){
			this.input.style.display="inline";
		}else{
			this.input.style.display="inline-block";
		}
		this.createInnerInput();
		this.createButton();
		this.input.appendChild(this.innerInput);
		this.input.appendChild(this.btn1);
		if(this.btn2){
			this.input.appendChild(this.btn2);
		}
	},
	createInnerInput:function(){
		this.innerInput = null;
		if(this.isIE){
			this.innerInput = document.createElement("<input type='text' name='"+this.name+"'>");
		}else{
			this.innerInput = document.createElement("INPUT");
			this.innerInput.type = "text";
			this.innerInput.setAttribute("name",this.name);
		}
		this.innerInput.id = this.id;
		this.innerInput.value = this.defaultValue;
		if (this.readOnly)
		{
			this.innerInput.readOnly = true;
			this.innerInput.contentEditable = false;
		}	
		this.innerInput.title=this.title;
		this.innerInput.className=this.className;
//		this.setInnerInputWidth();
	},
//	setInnerInputWidth:function(){
//		var width = this.width;
//		if(this.btn1Display!="none"){
//			width=width-jQuery(this.btn1).width()-2;
//		}
//		if(this.btn2Display!="none"){
//			width=width-jQuery(this.btn2).width()-2;
//		}
//		this.innerInput.style.width=width+"px";
//	},
	createButton:function(){
		this.btn1 = document.createElement("INPUT");
		this.btn1.value=this.btn1Value;
		this.btn1.type="button";
		this.btn1.className=this.btn1Class;
		this.btn1.style.display=this.btn1Display;
		this.btn1.id=(this.btn1Id?this.btn1Id:(this.id+"_btn1"));
		if(this.isTwoBtn){
			this.btn2 = document.createElement("INPUT");
			this.btn2.className=this.btn2Class;
			this.btn2.type="button";
			this.btn2.value=this.btn2Value;
			this.btn2.style.display=this.btn2Display;
			this.btn2.id=(this.btn2Id?this.btn2Id:(this.id+"_btn2"));
		}
	},
	reset:function(){
		this.innerInput.value=this.defaultValue;
	},
	clear:function(){
		this.innerInput.value="";
	},
	observeInnerInput:function(eventName,handler){
		Event.observe(this.innerInput,eventName,handler);
	},
	observeBtn1:function(eventName,handler){
		Event.observe(this.btn1,eventName,handler);
	},
	observeBtn2:function(eventName,handler){
		Event.observe(this.btn2,eventName,handler);
	},
	showBtn2:function(){
		this.btn2Display="";
		this.hideOrShowBtn(this.btn2,true);
	},
	hideBtn2:function(){
		this.btn2Display="none";
		this.hideOrShowBtn(this.btn2,false);
	},
	showBtn1:function(){
		this.btn1Display="";
		this.hideOrShowBtn(this.btn1,true);
	},
	hideBtn1:function(){
		this.btn1Display="none";
		this.hideOrShowBtn(this.btn1,false);
	},
	hideOrShowBtn:function(btn,isShow){
		if(btn){
			btn.style.display=(isShow?"":"none");
			//this.setInnerInputWidth();
		}
	}
};
function TransVirtualInputFromDIV(){
	$$(".virtualInput").each(function(ele){
		var  input=new Milan.widget.VirtualInput();
		input.setId(ele.id||"");
		input.setName(ele.getAttribute("name")||ele.id||"");
		input.setDefaultValue(ele.getAttribute("defaultValue")||"");
		input.setWidth(ele.getAttribute("inputWidth")||120);
		input.setTitle(ele.getAttribute("title")||"");
		input.setClassName(ele.getAttribute("inputClass")||"input");
		input.setDivClassName(ele.getAttribute("divClassName")||"div_input");
		input.setReadOnly(ele.getAttribute("readonly")||false);
		input.setBtn1Id(ele.getAttribute("btn1Id")||"");
		input.setBtn2Id(ele.getAttribute("btn2Id")||"");
		input.setBtn1Class(ele.getAttribute("btn1Class")||"search_btn1");
		input.setBtn2Class(ele.getAttribute("btn2Class")||"search_btn2");
		input.setBtn1Value(ele.getAttribute("btn1Value")||"");
		input.setBtn2Value(ele.getAttribute("btn2Value")||"");
		input.setBtn1Display(ele.getAttribute("btn1Display")||"");
		input.setBtn2Display(ele.getAttribute("btn2Display")||"");
		input.setIsTwoBtn(ele.getAttribute("isTwoBtn")||true);
		input.setPlaceholder(ele);
		input.transfer();
		/*if(input.getDefaultValue()){
			input.showBtn2();
		}*/
		var clearEvent = ele.getAttribute("clear")||false;
		input.observeBtn2("click",function(e){if(clearEvent){input.clear();input.hideBtn2();}var clickFun=ele.getAttribute("btn2Click");
		 if(clickFun&&clickFun.indexOf("event")>0){
		 	var args = clickFun.substring(clickFun.indexOf("(")+1,clickFun.length-1);
		 	args = args.split(",");
		 	for(var i=0;i<args.length;i++){
		 		if(args[i]=="event"){
		 			args[i] = e;
		 		}else{
		 			args[i] = eval(args[i]);
		 		}
		 	}
		 	eval(clickFun.substring(0,clickFun.indexOf("("))).apply(null,args)
		 }else{
		 	eval(clickFun||"");
		 }
		});
		var btn1Click = function(){eval(ele.getAttribute("btn1Click")||"");}
		input.observeBtn1("click",btn1Click);
		var keypress = ele.getAttribute("keypress")||true;
		if(keypress){
			input.observeInnerInput("keypress",function(e){e= e||event;if(e.keyCode == 13){btn1Click();}});
		}
	});
}